<?php
class Country extends ORDataObject{
	var $country_id	=	'';
	var $vi_name	=	'';
	var $en_name	=	'';
	var $disable	=	'';

	var	$_table		=	'country';
	var	$_key		=	'country_id';

	var $_internalName	=	'Country';
	const VN_COUNTRY_ID = 1;

	public function Country($db = null){
		parent::ORDataObject($db);
	}

	public function setup($id = 0){
		$this->set('id',$id);
		if($id > 0){
			$this->populate();
		}
	}

	public function populate(){
		parent::populate('country_id');
	}

	public function getId(){
		return $this->country_id;
	}

	public function setName($name, $culture = 'vi-VN'){
		$culprefix=strtolower(substr($culture,0,2));
		if($culprefix != 'en'){
			$varName	=	$culprefix."_name";
		}
		else{
			$varName	=	'name';
		}
		$this->set($varName, $name);
	}

	public static function getIdsByName($name, $culture = 'vi-VN'){
		$culprefix=strtolower(substr($culture,0,2));
		if($culprefix != 'en'){
			$varName	=	$culprefix."_name";
		}
		else{
			$varName	=	'name';
		}
		$obj	=	Celini::newORDO('Country');
		$ds 	=	& new Datasource_sql();
		$sql 	=	array(
            'cols'		=> "country_id",
            'from'		=> $obj->_table,
        	'where'		=> "$varName='$name'",
            'orderby'	=> 'country_id'
            );
            $ds->setup($obj->_db,$sql);
            return $ds->toArray('country_id');
	}

	public function getCountryList() {
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
		$ds =& new Datasource_sql();
		$sql = array(
            'cols'		=> "country_id, $field as name, disable",
            'from'		=> "$this->_table",
            'orderby'	=> 'name, disable'
            );
            $ds->setup($this->_db,$sql);
            return $ds->toArray();
	}
	
	public function getCountryListForSelect() {
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
		$ds =& new Datasource_sql();
		$sql = array(
            'cols'		=> "country_id, $field AS name",
            'from'		=> "$this->_table",
            'orderby'	=> 'name, disable',
			'where'		=> 'disable = 0'
            );
            $ds->setup($this->_db,$sql);
            return $ds->toArray('country_id', 'name');
	}

	public function deleteCountry($id) {
		$sql = "delete from $this->_table where $this->_key = $id";
		$this->_execute($sql);
	}

	public function disableCountry($id) {
		$sql = "update $this->_table set disable = 1 where $this->_key = $id";
		$this->_execute($sql);
	}

	public function enableCountry($id) {
		$sql = "update $this->_table set disable = 0 where $this->_key = $id";
		$this->_execute($sql);
	}

}
?>